const MyPromise = require('./4_myPromise');

let promise = new MyPromise((resolve, reject) => {
  //resolve('成功')
  //reject('失败')
  setTimeout(() => {
    resolve('成功')
  }, 0);
})

// promise.then(value => console.log(`then ${value}`))
//   .then(() => console.log('then.then'))
//   .catch(error => console.log(`error ${error}`))

// promise.then(value => console.log(`第二个then ${value}`))

const delay = (time = 500) => {
  return new MyPromise(resolve => {
    setTimeout(() => {
      resolve(`delay ${time}`)
    }, time);
  })
}

const throwException = (time = 500) => {
  return new MyPromise((resolve, reject) => {
    setTimeout(() => {
      reject(`throw exception ${time}`)
    }, time);
  })
}

promise.then(() => delay())
  .then(() => throwException(100))
  .finally(() => console.log('promise finally'))

MyPromise.all([delay(100), '1', delay(300)]).then(value => console.log(`all ${value}`))
MyPromise.race([delay(200), delay(1000), throwException(100)]).then(value => console.log(`race ${value}`))